package com.lzy.shops.service.impl;

import com.lzy.shops.bean.Area;
import com.lzy.shops.bean.GoodsType;
import com.lzy.shops.bean.Search;
import com.lzy.shops.mapper.GoodsTypeMapper;
import com.lzy.shops.service.GoodsTypeService;
import com.lzy.shops.utils.exception.ActionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {

    @Autowired
    private GoodsTypeMapper goodsTypeMapper;

    @Override
    public List<GoodsType> findAll(Integer id) throws ActionException {
        List<GoodsType> allChildren = null;
        allChildren = goodsTypeMapper.findChildren(id);
        if(allChildren!=null){
            digui(allChildren);
        }else{
            throw new ActionException("操作异常");
        }
        return allChildren;
    }

    @Override
    public Integer addGoodsType(GoodsType goodsType) {
        return goodsTypeMapper.addGoodsType(goodsType);
    }

    @Override
    public Map<String,String> delGoodsType(Integer id) throws ActionException {
        List<GoodsType> children = goodsTypeMapper.findChildren(id);
        Integer count = 0;
        Map<String,String> info = new HashMap<>();
        if(children.size()>=1){ // 存在子地区
            for (GoodsType child : children) {
                Integer integer = goodsTypeMapper.delGoodsType(child.getId().intValue());
                if(integer==1){
                    count++;
                }else{
                    throw new ActionException("删除失败");
                }
            }
            Integer integer = goodsTypeMapper.delGoodsType(id);
            if(integer!=1){
                throw new ActionException("删除失败");
            }
            info.put("msg","成功删除一个父地区,其中包含了"+count+"个子地区");
        }else{
            Integer integer = goodsTypeMapper.delGoodsType(id);
            if(integer!=1){
                throw new ActionException("删除失败");
            }
            info.put("msg","成功删除");
        }
        return info;
    }

    @Override
    public Integer findMaxId(Integer id) {
        return goodsTypeMapper.findMaxId(id);
    }

    @Override
    public Integer updateGoodsType(GoodsType goodsType) {
        return goodsTypeMapper.updateGoodsType(goodsType);
    }

    @Override
    public List<GoodsType> defaultData() {
        return goodsTypeMapper.defaultData();
    }

    private void digui(List<GoodsType> citys) {
        List<GoodsType> retList = new ArrayList<>();
        for (GoodsType c : citys) {
            retList = goodsTypeMapper.findChildren(c.getId().intValue());
            if (retList.size() > 0) {
                c.setIsLeaf(false);
                c.setChildren(retList);
                if(retList.size()>=1){
                    digui(retList);
                }
            }else{
                c.setIsLeaf(true);
            }
        }
    }
}
